package com.ltba.rf.control;

import com.ltba.rf.control.beeper.BeeperManager;
import com.ltba.rf.control.file.AppData;
import com.ltba.rf.control.html.EarthquakeSenser;
import com.ltba.rf.control.thread.RealTimeData;
import com.ltba.rf.control.thread.RealTimeManager;
import com.ltba.rf.view.BrowserPanel;

public class RedFeatherManager implements Runnable {

	private AppData appData;
	private BeeperManager bm;
	private BrowserPanel bp;

	public RedFeatherManager(AppData appData, BrowserPanel bp) {
		this.appData = appData;
		bm = new BeeperManager();
		this.bp = bp;
	}

	private boolean isNeedToAlert() {

		RealTimeData oldData = RealTimeManager.getInstance().getData();
		RealTimeData newData;
		try {
			newData = EarthquakeSenser.getData();
			if (null == oldData) {
				RealTimeManager.getInstance().setData(newData);
			}

			// new info and max >= degree
			if (null != oldData && null != newData) {
				if (!oldData.getKey().equals(newData.getKey())) {
					RealTimeManager.getInstance().setData(newData);
					bp.setPage(BrowserPanel.DEFAULT_URL);
					if (!newData.isIndexMorethanMax(appData.getLevel() + 1)) {
						return true;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}

	@Override
	public void run() {
		if (isNeedToAlert()) {
			bm.beep(appData.getAlerm(), 10);
		}
	}
}
